package edu.gatech.cs2340.sharks.fms;

/**
 * @author Steven Mitchell
 * 
 * This activity obtains search parameters from FilterSearchActivity.java,
 * sends them to the database, and displays all search results.
 */

import java.util.ArrayList;
import edu.gatech.cs2340.sharks.fms.data.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;

public class ShowFilteredResultsActivity extends Activity
{
	/**
	 * The item name parameter passed from FilterSearchActivity.java
	 */
	private String itemName;
	
	/**
	 * The location parameter passed from FilterSearchActivity.java
	 */
	private String location;
	
	/**
	 * The starting date parameter passed from FilterSearchActivity.java
	 */
	private String startDate;
	
	/**
	 * The end date parameter passed from FilterSearchActivity.java
	 */
	private String endDate;
	
	/**
	 * An int representing whether the user is searching for
	 * lost items (1) or found items (2), passed from FilterSearchActivity.java
	 */
	private int itemBinary;
	
	/**
	 * The lost or found status of an item, determined by the itemBinary
	 */
	private Item.Status itemStatus;
	
	/**
	 * The string of tags to filter by, passed from FilterSearchActivity.java
	 */
	private String tagString;
	
	/**
	 * The listview object from the layout that displays the search results.
	 */
	private ListView showResults;
	
	/**
	 * The current instance of the activity, used to make sure there is only one instance of this activity running at a time.
	 */
	private static Activity instance = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		if (instance != null) instance.finish();
		instance = this;
		
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_show_filtered_results);
		
		final TextView statusText = (TextView) findViewById(R.id.statusText);
		
		Intent intent = getIntent();
		Bundle extras = intent.getExtras(); 
		itemName = extras.getString("itemName");
		location = extras.getString("location");
		startDate = extras.getString("startDate");
		endDate = extras.getString("endDate");
		itemBinary = extras.getInt("itemBinary");
		tagString = extras.getString("tagString");
		
		if (itemBinary == 1) itemStatus = Item.Status.LOST;
		else if (itemBinary == 2) itemStatus = Item.Status.FOUND;
		else itemStatus = null;
		
		List<Tag> tags = new ArrayList<Tag>();
		Packet<List<Tag>> tagList = DBMgr.allTags();
		if (tagList.status.code() != Status.Code.OK) statusText.setText(tagList.status.toString());
		else 
		{
			if (tagString != null && tagString.equals("") != true)
			{
				String[] tagNames = tagString.split(",");
				for (int i = 0; i < tagNames.length; i++)
				{
					boolean found = false;
					for (Tag t : tagList.value) if (t.name.equals(tagNames[i]))
					{
						found = true;
						tags.add(t);
						break;
					}
					if (! found) statusText.setText("You have entered one or more invalid tags.");
				}
			}

			if ("--".equals(startDate)) startDate = null;
			if ("--".equals(endDate)) endDate = null;
			Packet<List<Item>> filtered = DBMgr.search(itemName, location, startDate, endDate, itemStatus, tags);
			if (filtered.status.code() == Status.Code.OK)
			{
				List<Item> items = filtered.value;
				Item[] results = new Item[items.size()];
				for (int i = 0; i < items.size(); i++) results[i] = items.get(i);
				
				showResults = (ListView) findViewById(R.id.results);
				ItemAdapter adaptItems = new ItemAdapter(this, R.layout.listview_search_row, results);
				showResults.setClickable(true);
				showResults.setAdapter(adaptItems);
			}
			else Info.alert(this, filtered.status.toString());
		}
	}

	/**
	 * This method returns the user to FilterSearchActivity.java so that they may perform another search.
	 * 
	 * @param view The button that runs this method when clicked
	 */
	public void returnToSearch(View view)
	{
		showResults.setAdapter(null);
		Intent back = new Intent(this, FilterSearchActivity.class);
		startActivity(back);
		finish();
	}
	
	/**
	 * This method opens the item details page for an item.
	 * 
	 * @param view The button that runs this method when clicked
	 */
	public void itemDetails(View view)
	{
		int itemID = (Integer) view.getTag(); 
		Intent toItemDetails = new Intent(this, ViewItemActivity.class);
		toItemDetails.putExtra("ItemID", itemID); 
		startActivity(toItemDetails); 
	}
}
